//
//  MYTabBar.swift
//  MYWeiBo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

class MYTabBar: UITabBar {
    //定义一个闭包，传递加号按钮的点击事件
    var composeCallBack:(() -> ())?

 //布局子控件，添加中间的加号按钮
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置加号按钮
        let width = self.frame.size.width/5
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.size.height)
        var index = 0
        for view in self.subviews {
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: self.frame.size.height)
                index++
                if index == 2{
                    index++
                }
            }
        }
    }
    /**
     *加号的点击事件
     */
    func composeClick(button:UIButton){
    
        composeCallBack?()
    }
    
    /**
    *懒加载添加中间加号按钮
    */
    lazy var composeButton:UIButton = {
        //添加按钮
        var button = UIButton()
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //添加到tabbar中
        self.addSubview(button)
        //添加点击事件
        button.addTarget(self, action:"composeClick:", forControlEvents: UIControlEvents.TouchUpInside)

        return button
 
    }()

}
